﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class AddProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateProjects();

            }

        }

        private void PopulateProjects()
        {
            try
            {
                ProjectManager manager = new ProjectManager();
                List<Project> projects = new List<Project>();
                projects = manager.GetAllProjects();
                ProjectsGridview.DataSource = projects;
                ProjectsGridview.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveProject(object sender, EventArgs e)
        {
            try
            {
                Project aProject = new Project();
                ProjectManager manager = new ProjectManager();
                aProject.ProjectName = txtProjectName.Value;
                aProject.startingDate = Convert.ToDateTime(txtStartingdate.Value);
                aProject.ClosingDate = Convert.ToDateTime(txtClosingdate.Value);
                aProject.TotalEmployee = Convert.ToInt32(txtEmployeeNumber.Value);
                aProject.ProjectDescription = txtProjectdescription.Value;
                aProject.Location = txtProjectLocation.Value;
                string msg = manager.SaveProject(aProject);
                msgLabel.Text = msg;
                if (msg.Contains("Successfully"))
                {
                    ClearInputs();
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Font.Bold = true;
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Font.Bold = true;
                }
                PopulateProjects();
            }
            catch (Exception ex)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Font.Bold = true;
                msgLabel.Text = "Please Check all required field";
            }
        }

        protected void ProjectsGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ProjectsGridview.PageIndex = e.NewPageIndex;
            PopulateProjects();

        }
        private void ClearInputs()
        {
            txtProjectName.Value = "";
            txtStartingdate.Value = "";
            txtClosingdate.Value = "";
            txtEmployeeNumber.Value = "";
            txtProjectLocation.Value = "";
            txtProjectdescription.Value = "";

        }
        protected void ProjectsGridview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            ProjectsGridview.EditIndex = -1;
            PopulateProjects();

        }

        protected void ProjectsGridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                ProjectManager manager = new ProjectManager();
                Project project = new Project();
                int id = Convert.ToInt32(((Label)ProjectsGridview.Rows[e.RowIndex].FindControl("lblId")).Text);
                msgLabel.Text = manager.DeleteProjects(id);
                PopulateProjects();

            }
            catch (Exception ex)
            {
                msgLabel.Text = ex.Message;
                msgLabel.ForeColor = Color.Red;
            }
        }

        protected void ProjectsGridview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ProjectsGridview.EditIndex = e.NewEditIndex;
            PopulateProjects();

        }

        protected void ProjectsGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                ProjectManager manager = new ProjectManager();
                Project project = new Project();
                project.Id = Convert.ToInt32(((Label)ProjectsGridview.Rows[e.RowIndex].FindControl("lblId")).Text);
                project.ProjectName = ((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtPName")).Text;
                project.startingDate = Convert.ToDateTime(((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtStartDate")).Text);
                project.ClosingDate = Convert.ToDateTime(((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtCloseDate")).Text);
                project.TotalEmployee = Convert.ToInt32(((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtxTotalEmp")).Text);
                project.Location = ((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtProjectLocationgv")).Text;
                project.ProjectDescription = ((TextBox)ProjectsGridview.Rows[e.RowIndex].FindControl("txtDescription")).Text;
                msgLabel.Text = manager.UpdateProject(project);
                ProjectsGridview.EditIndex = -1;
                PopulateProjects();
                msgLabel.Text = "Project Updated successfully";
                msgLabel.ForeColor = Color.Green;

            }
            catch (Exception ex)
            {
                msgLabel.Text = "Sorry,Project not Updated";
                msgLabel.ForeColor = Color.Red;
            }


        }

    }
}